﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Workbench.Presentation;

namespace Workbench.Presentation
{
    /// <summary>
    /// Interaction logic for ProjectTreeView.xaml
    /// </summary>
    public partial class ProjectTreeView : UserControl
    {
        public ProjectTreeView()
        {
            InitializeComponent();
        }

        private void TreeView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);

            if (treeViewItem != null)
            {
                treeViewItem.IsSelected = true;
            }
        }

        static TreeViewItem VisualUpwardSearch(DependencyObject source)
        {
            while (source != null && !(source is TreeViewItem))
                source = VisualTreeHelper.GetParent(source);

            return source as TreeViewItem;
        }

        private void AddExistingDirectory_Click(object sender, RoutedEventArgs e)
        {
            var fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.SelectedPath = UserSettings.LastUsedDirectory;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                UserSettings.LastUsedDirectory = fbd.SelectedPath;
                AddExistingDirectoryCommand.Execute(fbd.SelectedPath);
            }
        }

        private void AddExistingFile_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog()
            {
                DefaultExt = ".txt",
                Filter = "Text Files (*.txt)|*.txt",
                ValidateNames = true,
                InitialDirectory = UserSettings.LastUsedDirectory,
                CheckFileExists = true,
                CheckPathExists = true,
                AddExtension = true,
                Multiselect = false

            };

            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                UserSettings.LastUsedDirectory = Path.GetDirectoryName(dlg.FileName);
                AddExistingFileCommand.Execute(dlg.FileName);
            }
        }

        #region AddExistingFileCommand property

        private static readonly DependencyProperty AddExistingFileCommandProperty = DependencyProperty.RegisterAttached(
          "AddExistingFileCommand",
          typeof(ICommand),
          typeof(ProjectTreeView),
          new PropertyMetadata());

        public ICommand AddExistingFileCommand
        {
            get { return (ICommand)GetValue(AddExistingFileCommandProperty); }
            set { SetValue(AddExistingFileCommandProperty, value); }
        }

        #endregion

        #region AddExistingDirectoryCommand property

        private static readonly DependencyProperty AddExistingDirectoryCommandProperty = DependencyProperty.RegisterAttached(
          "AddExistingDirectoryCommand",
          typeof(ICommand),
          typeof(ProjectTreeView),
          new PropertyMetadata());

        public ICommand AddExistingDirectoryCommand
        {
            get { return (ICommand)GetValue(AddExistingDirectoryCommandProperty); }
            set { SetValue(AddExistingDirectoryCommandProperty, value); }
        }

        #endregion
    }
}
